/*
 * @FileName: [ReportController.java]
 * 
 * @Package com.bcinfo.iccard.admin.iccard.controller
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with RKY.
 * 
 * $Rev$
 * $LastChangedDate$
 * $LastChangedBy$
 * 
 * @category ICCARD
 * 
 * @version 1.1
 * 
 * @author $Author$
 * 
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
 */

package com.bcinfo.iccard.admin.iccard.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bcinfo.iccard.admin.controller.AbstractController;
import com.bcinfo.iccard.admin.util.ExcelUtil;
import com.bcinfo.iccard.exception.InvalidParametersException;
import com.bcinfo.iccard.model.ComboCardDailyEarningsDetail;
import com.bcinfo.iccard.model.ComboCardTotalEarnings;
import com.bcinfo.iccard.model.DaliyTotalEarnings;
import com.bcinfo.iccard.model.EndComboCardEarningsToMerchantDetail;
import com.bcinfo.iccard.model.Excelable;
import com.bcinfo.iccard.model.ToMerchantComboCardEarningsDetail;
import com.bcinfo.iccard.model.ToMerchantComboCardTotalEarnings;
import com.bcinfo.iccard.model.ToMerchantDaliyEarningsDetail;
import com.bcinfo.iccard.model.ToMerchantRechargeableCardTotalEarnings;
import com.bcinfo.iccard.service.ComboCardService;
import com.bcinfo.iccard.service.FinancialReportService;
import com.bcinfo.iccard.service.MerchantService;
import com.bcinfo.iccard.util.PaginationList;

/**
 * 报表controler <br/>
 * <p>
 * Description:<br/>
 * <p>
 * 1、消费结算日报表<br/>
 * 2、消费结算月报表<br/>
 * 3、消费结算季报表<br/>
 * 4、消费结算年报表<br/>
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 * 代码使用示例
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/iccard/report/financial")
public class FinancialReportController extends AbstractController
{

    // 临时文件目录
    private static final String    TEMP_FILE_PATH  = "/iccard/report/temp/";

    private static final int       EXPORT_MAX_ROWS = 65535;

    @Autowired
    private FinancialReportService _financialReportService;
    @Autowired
    private MerchantService        _merchantService;

    @Autowired
    private ComboCardService       _comboCardService;

    // 财务--消费结算收益日报
    @ResponseBody
    @RequestMapping("/settlement/daily")
    public Map<String, Object> daily(String day, String sellerCode)
    {
        String param = "";
        if (StringUtils.isNotBlank(sellerCode))
        {
            String[] ss = sellerCode.split(",");
            param = ss[0];
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ToMerchantDaliyEarningsDetail> plist = _financialReportService.dailyReport(param, day,
                    getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ToMerchantDaliyEarningsDetail> plist1 = _financialReportService.dailyReport(param, day, 1,
                    EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 财务--消费结算收益月报
    @ResponseBody
    @RequestMapping("/settlement/monthly")
    public Map<String, Object> month(String month, String sellerCode)
    {
        String param = "";
        if (StringUtils.isNotBlank(sellerCode))
        {
            String[] ss = sellerCode.split(",");
            param = ss[0];
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ToMerchantDaliyEarningsDetail> plist = _financialReportService.monthlyReport(param, month,
                    getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ToMerchantDaliyEarningsDetail> plist1 = _financialReportService.monthlyReport(param, month,
                    1, EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 财务--消费结算收益季度报表
    @ResponseBody
    @RequestMapping("/settlement/quarterly")
    public Map<String, Object> quarter(String quarter, String sellerCode)
    {
        String param = "";
        if (StringUtils.isNotBlank(sellerCode))
        {
            String[] ss = sellerCode.split(",");
            param = ss[0];
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ToMerchantDaliyEarningsDetail> plist = _financialReportService.quarterlyReport(param,
                    quarter, getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ToMerchantDaliyEarningsDetail> plist1 = _financialReportService.quarterlyReport(param,
                    quarter, 1, EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/settlement/yearly")
    public Map<String, Object> year(String year, String sellerCode)
    {
        String param = "";
        if (StringUtils.isNotBlank(sellerCode))
        {
            String[] ss = sellerCode.split(",");
            param = ss[0];
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ToMerchantDaliyEarningsDetail> plist = _financialReportService.yearlyReport(param, year,
                    getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ToMerchantDaliyEarningsDetail> plist1 = _financialReportService.yearlyReport(param, year, 1,
                    EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/settlement/query")
    public Map<String, Object> query(String beginTime, String endTime, String sellerCode)
    {
        String param = "";
        if (StringUtils.isNotBlank(sellerCode))
        {
            String[] ss = sellerCode.split(",");
            param = ss[0];
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ToMerchantDaliyEarningsDetail> plist = _financialReportService.queryReport(param, beginTime,
                    endTime, getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ToMerchantDaliyEarningsDetail> plist1 = _financialReportService.queryReport(param,
                    beginTime, endTime, 1, EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/settlement/querychart")
    public Map<String, Object> queryForChat(String beginTime, String endTime, String sellerCode)
    {
        String param = "";
        if (StringUtils.isNotBlank(sellerCode))
        {
            String[] ss = sellerCode.split(",");
            param = ss[0];
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            // 指定商家时，为商家销售额日变化曲线情况，未指定商家时，为所有商家销售总额日变化曲线情况

            List<ToMerchantDaliyEarningsDetail> list = _financialReportService.queryReportForChart(param, beginTime,
                    endTime);
            Map<String, ToMerchantDaliyEarningsDetail> total = new TreeMap<String, ToMerchantDaliyEarningsDetail>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar s = Calendar.getInstance();
            Calendar e = Calendar.getInstance();

            while (s.getTimeInMillis() <= e.getTimeInMillis())
            {
                //
                total.put(sdf.format(s.getTimeInMillis()), new ToMerchantDaliyEarningsDetail());
                s.add(Calendar.DAY_OF_MONTH, 1);
            }
            for (ToMerchantDaliyEarningsDetail t : list)
            {
                total.put(sdf.format(t.getDaliyEarningsDate()), t);
            }

            float[] totals_sales = new float[total.size()];// 每日销售总额
            float[] packages_sales = new float[total.size()];// 套餐卡每日销售
            float[] prepaids_sales = new float[total.size()];// 充值卡每日销售
            float packages_sales_total = 0.0f;// 套餐卡累计销售总额
            float prepaids_sales_total = 0.0f;// 充值卡累计销售总额

            float[] totals_profit = new float[total.size()];// 每日收益总额
            float[] packages_profit = new float[total.size()];// 套餐卡每日收益
            float[] prepaids_profit = new float[total.size()];// 充值卡每日收益
            float packages_profit_total = 0.0f;// 套餐卡累计收益总额
            float prepaids_profit_total = 0.0f;// 充值卡累计收益总额
            int i = 0;
            for (ToMerchantDaliyEarningsDetail t : total.values())
            {
                totals_sales[i] = t.getComboCardExpenseAmount() + t.getRechargeableCardExpenseAmount();
                packages_sales[i] = t.getComboCardExpenseAmount();
                prepaids_sales[i] = t.getRechargeableCardExpenseAmount();
                packages_sales_total += t.getComboCardExpenseAmount();
                prepaids_sales_total += t.getRechargeableCardExpenseAmount();

                totals_profit[i] = t.getComboCardRealEarnings() + t.getRechargeableCardRealTotalEarnings();
                packages_profit[i] = t.getComboCardRealEarnings();
                prepaids_profit[i] = t.getRechargeableCardRealTotalEarnings();
                packages_profit_total += t.getComboCardRealEarnings();
                prepaids_profit_total += t.getRechargeableCardRealTotalEarnings();
                i++;
            }

            // //日期
            map.put("days", total.keySet().toArray());
            map.put("totals_sales", totals_sales);
            map.put("packages_sales", packages_sales);
            map.put("prepaids_sales", prepaids_sales);
            map.put("packages_sales_total", 100 * packages_sales_total / (packages_sales_total + prepaids_sales_total));
            map.put("prepaids_sales_total", 100 * prepaids_sales_total / (packages_sales_total + prepaids_sales_total));
            //
            map.put("totals_profit", totals_profit);
            map.put("packages_profit", packages_profit);
            map.put("prepaids_profit", prepaids_profit);
            map.put("packages_profit_total", 100 * packages_profit_total
                    / (packages_profit_total + prepaids_profit_total));
            map.put("prepaids_profit_total", 100 * prepaids_profit_total
                    / (packages_profit_total + prepaids_profit_total));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/settlement/querychart2")
    public Map<String, Object> queryForChat2(String beginTime, String endTime, String sellerCode)
    {
        String param = "";
        if (StringUtils.isNotBlank(sellerCode))
        {
            String[] ss = sellerCode.split(",");
            param = ss[0];
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            // 指定商家时，为商家销售额日变化曲线情况，未指定商家时，为所有商家销售总额日变化曲线情况

            List<ToMerchantDaliyEarningsDetail> list = _financialReportService.queryReportForChart(param, beginTime,
                    endTime);
            Map<String, ToMerchantDaliyEarningsDetail> total = new TreeMap<String, ToMerchantDaliyEarningsDetail>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar s = Calendar.getInstance();
            Calendar e = Calendar.getInstance();

            while (s.getTimeInMillis() <= e.getTimeInMillis())
            {
                //
                total.put(sdf.format(s.getTimeInMillis()), new ToMerchantDaliyEarningsDetail());
                s.add(Calendar.DAY_OF_MONTH, 1);
            }
            for (ToMerchantDaliyEarningsDetail t : list)
            {
                total.put(sdf.format(t.getDaliyEarningsDate()), t);
            }

            float[] totals_sales = new float[total.size()];// 每日销售总额
            float[] packages_sales = new float[total.size()];// 套餐卡每日销售
            float[] prepaids_sales = new float[total.size()];// 充值卡每日销售
            float packages_sales_total = 0.0f;// 套餐卡累计销售总额
            float prepaids_sales_total = 0.0f;// 充值卡累计销售总额

            float[] totals_profit = new float[total.size()];// 每日收益总额
            float[] packages_profit = new float[total.size()];// 套餐卡每日收益
            float[] prepaids_profit = new float[total.size()];// 充值卡每日收益
            float packages_profit_total = 0.0f;// 套餐卡累计收益总额
            float prepaids_profit_total = 0.0f;// 充值卡累计收益总额
            int i = 0;
            for (ToMerchantDaliyEarningsDetail t : total.values())
            {
                totals_sales[i] = t.getComboCardExpenseAmount() + t.getRechargeableCardExpenseAmount();
                packages_sales[i] = t.getComboCardExpenseAmount();
                prepaids_sales[i] = t.getRechargeableCardExpenseAmount();
                packages_sales_total += t.getComboCardExpenseAmount();
                prepaids_sales_total += t.getRechargeableCardExpenseAmount();

                totals_profit[i] = t.getComboCardExpectedEarnings() + t.getRechargeableCardExpectedEarnings();
                packages_profit[i] = t.getComboCardExpectedEarnings();
                prepaids_profit[i] = t.getRechargeableCardExpectedEarnings();
                packages_profit_total += t.getComboCardExpectedEarnings();
                prepaids_profit_total += t.getRechargeableCardExpectedEarnings();
                i++;
            }

            // //日期
            map.put("days", total.keySet().toArray());
            map.put("totals_sales", totals_sales);
            map.put("packages_sales", packages_sales);
            map.put("prepaids_sales", prepaids_sales);
            map.put("packages_sales_total", 100 * packages_sales_total / (packages_sales_total + prepaids_sales_total));
            map.put("prepaids_sales_total", 100 * prepaids_sales_total / (packages_sales_total + prepaids_sales_total));
            //
            map.put("totals_profit", totals_profit);
            map.put("packages_profit", packages_profit);
            map.put("prepaids_profit", prepaids_profit);
            map.put("packages_profit_total", 100 * packages_profit_total
                    / (packages_profit_total + prepaids_profit_total));
            map.put("prepaids_profit_total", 100 * prepaids_profit_total
                    / (packages_profit_total + prepaids_profit_total));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 收益额统计
    @ResponseBody
    @RequestMapping("/income/report")
    public Map<String, Object> income(String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<DaliyTotalEarnings> plist = _financialReportService.incomeReport(beginTime, endTime,
                    getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<DaliyTotalEarnings> plist1 = _financialReportService.incomeReport(beginTime, endTime, 1,
                    EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return map;
    }

    // 收益额统计 汇总
    @ResponseBody
    @RequestMapping("/income/reportSum")
    public Map<String, Object> incomeReportSum(String beginTime, String endTime)
    {

        Map<String, Object> map = new HashMap<String, Object>();
        DaliyTotalEarnings t = _financialReportService.incomeReportSum(beginTime, endTime);
        if (t != null)
        {
            map.put("incomeTotal", t.getRealEarningsSum());
            map.put("settlementTotal", t.getDaliyWaitAmountSum());
            map.put("expectedTotal", t.getExpectedTotalEarnings());
        }
        return map;
    }

    // 收益额统计检索
    @ResponseBody
    @RequestMapping("/income/query")
    public Map<String, Object> incomeQuery(String sellerIds, String sellerNames, String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            ToMerchantDaliyEarningsDetail t = _financialReportService.incomeReportQuery(sellerIds, beginTime, endTime);

            // // 套餐卡实际收益总额
            // map.put("packageTotal", t.getComboCardRealEarnings());
            // // 充值卡实际收益总额
            // map.put("prepaidTotal", t.getRechargeableCardRealTotalEarnings());
            // // 实际收益总额累积
            // map.put("incomeTotal", t.getRealEarningsSum());
            // map.put("reportDates", beginTime + "~" + endTime);
            // map.put("sellerNames", sellerNames);
            ArrayList<ToMerchantDaliyEarningsDetail> list = new ArrayList<ToMerchantDaliyEarningsDetail>();
            list.add(t);
            map.put("rows", list);
            map.put("total", 1);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 套餐卡数据统计
    @ResponseBody
    @RequestMapping("/package/report")
    public Map<String, Object> packageReport(String packageId, String status, String beginTime, String endTime)
    {
        String param = "";
        if (StringUtils.isNotBlank(packageId))
        {
            String[] ss = packageId.split(",");
            param = ss[0];
        }
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isBlank(status))
            status = "-1";
        try
        {
            PaginationList<ComboCardTotalEarnings> plist = _financialReportService.packageReport(param, beginTime,
                    endTime, Integer.parseInt(status), getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ComboCardTotalEarnings> plist1 = _financialReportService.packageReport(param, beginTime,
                    endTime, Integer.parseInt(status), 1, EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 套餐卡数据统计
    @ResponseBody
    @RequestMapping("/package/detail")
    public Map<String, Object> packageDetail(String cardId, String sellerCode, String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ComboCardDailyEarningsDetail> plist = _financialReportService.packageDetail(cardId,
                    sellerCode, beginTime, endTime, getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ComboCardDailyEarningsDetail> plist1 = _financialReportService.packageDetail(cardId,
                    sellerCode, beginTime, endTime, 1, EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //
        }
        catch (InvalidParametersException e)
        {
            // e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/package/detailSum")
    public Map<String, Object> packageDetailSum(String cardId, String sellerCode, String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            ComboCardDailyEarningsDetail t = _financialReportService.packageDetailSum(cardId, sellerCode, beginTime,
                    endTime);
            // 实际收益累积总额
            if (t != null)
            {
                map.put("incomeTotal", t.getComboCardRealEarnings());
                // 待结算累积总额
                map.put("settlementTotal", t.getComboCardSettlementAmount());
                // 预期收益累积总额
                map.put("expectedTotal", t.getComboCardExpectedEarnings());
            }
        }
        catch (InvalidParametersException e)
        {
            // e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/package/actSum")
    public Map<String, Object> packageActSum(String packageId, String beginTime, String endTime, int status)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            ComboCardTotalEarnings t = _financialReportService.packageActSum(packageId, beginTime, endTime, status);
            // 实际收益累积总额
            if (t != null)
            {
                // 套餐卡销售总额
                map.put("salesTotal", t.getComboCardExperseAmount());
                // 实际收益总额
                map.put("incomeTotal", t.getComboCardRealEarnings());
                // 预期收益总额
                map.put("expectedTotal", t.getComboCardExpectedEarnings());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 活动结束套餐收益明细
    @ResponseBody
    @RequestMapping("/package/actDetail")
    public Map<String, Object> packageActDetail(String cardId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<EndComboCardEarningsToMerchantDetail> plist = _financialReportService.packageActDetail(
                    cardId, getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<EndComboCardEarningsToMerchantDetail> plist1 = _financialReportService.packageActDetail(
                    cardId, 1, EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //

        }
        catch (InvalidParametersException e)
        {
            // e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/package/actDetailCount")
    public int packageActDetailCount(String cardId)
    {

        try
        {
            return _financialReportService.packageActDetailCount(cardId);
        }
        catch (InvalidParametersException e)
        {
            e.printStackTrace();
        }

        return 0;
    }

    @ResponseBody
    @RequestMapping("/package/actDetailSum")
    public Map<String, Object> packageActDetailSum(String cardId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            EndComboCardEarningsToMerchantDetail t = _financialReportService.packageActDetailSum(cardId);
            // 实际收益累积总额
            if (t != null)
            {
                // 套餐卡销售总额
                map.put("salesTotal", t.getExpenseTotalAmount());
                // 实际收益总额
                map.put("incomeTotal", t.getComboCardRealAmount());
                // 结算总额
                map.put("settlementTotal", t.getComboCardSettlementAmount());
                // 预期收益总额
                map.put("expectedTotal", t.getExpectedAmountSum());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 商家收益列表
    @ResponseBody
    @RequestMapping("/package/seller")
    public Map<String, Object> seller(String sellerCode)
    {
        String param = "";
        if (StringUtils.isNotBlank(sellerCode))
        {
            String[] ss = sellerCode.split(",");
            param = ss[0];
        }

        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ToMerchantComboCardTotalEarnings> plist = _financialReportService.sellerIncome(param,
                    getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ToMerchantComboCardTotalEarnings> plist1 = _financialReportService.sellerIncome(param, 1,
                    EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 商家套餐收益明细
    @ResponseBody
    @RequestMapping("/package/sellerDetail")
    public Map<String, Object> sellerDetail(String dataId, String packageId, String beginTime, String endTime)
    {
        if (StringUtils.isBlank(dataId))
            dataId = "0";
        String param = "";
        if (StringUtils.isNotBlank(packageId))
        {
            String[] ss = packageId.split(",");
            param = ss[0];
        }

        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ToMerchantComboCardEarningsDetail> plist = _financialReportService.sellerDetail(dataId,
                    param, beginTime, endTime, getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls

            PaginationList<ToMerchantComboCardEarningsDetail> plist1 = _financialReportService.sellerDetail(dataId,
                    param, beginTime, endTime, 1, EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            //
            Map<String, String> alias = new LinkedHashMap<String, String>();
            alias.put("商家名称", "merchantName");
            alias.put("涉及商品", "productNames");
            alias.put("实际提取份数", "combcardExpenseCopies");
            alias.put("原价格", "productOrignalPrice");
            alias.put("优惠价格", "productFavorablePrice");
            alias.put("销售价格", "productSalePrice");
            alias.put("销售额", "expenseTotalAmount");
            alias.put("应付额", "payableAmount");
            alias.put("结算额", "comboCardSettlementAmount");
            alias.put("实际收益额", "comboCardRealAmount");
            alias.put("待结算额", "comboCardWaitAmountSum");
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH, alias);
            map.put("fileName", file);
            //

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 商家收益明细 汇总统计
    @ResponseBody
    @RequestMapping("/package/sellerDetailSum")
    public Map<String, Object> packageSellerDetailSum(String dataId, String packageId, String beginTime, String endTime)
    {
        if (StringUtils.isBlank(dataId))
            dataId = "0";
        String param = "";
        if (StringUtils.isNotBlank(packageId))
        {
            String[] ss = packageId.split(",");
            param = ss[0];
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            ToMerchantComboCardEarningsDetail t = _financialReportService.sellerDetailSum(dataId, param, beginTime,
                    endTime);
            // 实际收益累积总额
            if (t != null)
            {
                // 实际收益总额
                map.put("incomeTotal", t.getComboCardRealEarnings());
                // 预期收益总额
                map.put("expectedTotal", t.getComboCardExpectedEarnings());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/package/sellerDetailChat")
    public Map<String, Object> sellerDetailChat(String dataId, String beginTime, String endTime)
    {
        if (StringUtils.isBlank(dataId))
            dataId = "0";
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            // 指定商家时，为商家销售额日变化曲线情况，未指定商家时，为所有商家销售总额日变化曲线情况

            List<ToMerchantComboCardEarningsDetail> list = _financialReportService.sellerDetailChat(dataId, beginTime,
                    endTime);

            String[] packages = new String[list.size()];
            float[] totals_sales = new float[list.size()];// 每套餐销售总额
            float[] totals_income = new float[list.size()];// 每套餐实际收益
            float[] totals_expected = new float[list.size()];// 每套餐预期收益
            float sales_total = 0.0f;// 销售额
            float income_total = 0.0f;// 实际收益额
            float expected_total = 0.0f;// 预期收益额
            // 比例
            float[] sales_rate = new float[list.size()];// 销售额
            float[] income_rate = new float[list.size()];// 实际收益额
            float[] expected_rate = new float[list.size()];// 预期收益额 int i = 0;
            int i = 0;
            for (ToMerchantComboCardEarningsDetail t : list)
            {
                packages[i] = t.getComboCardName();
                totals_sales[i] = t.getComboCardExperseAmount();
                totals_income[i] = t.getComboCardRealEarnings();
                totals_expected[i] = t.getComboCardExpectedEarnings();
                sales_total += t.getComboCardExperseAmount();
                income_total += t.getComboCardRealEarnings();
                expected_total += t.getComboCardExpectedEarnings();
                i++;
            }
            for (int j = 0; j < packages.length; j++)
            {
                sales_rate[j] = 100 * totals_sales[j] / sales_total;
                income_rate[j] = 100 * totals_income[j] / income_total;
                expected_rate[j] = 100 * totals_expected[j] / expected_total;
            }
            //
            map.put("packages", packages);
            map.put("sales_rate", sales_rate);
            map.put("income_rate", income_rate);
            map.put("expected_rate", expected_rate);
            //
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 充值卡数据统计
    @ResponseBody
    @RequestMapping("/prepaid/report")
    public Map<String, Object> prepaidCardReport(String sellerCode)
    {
        String param = "";
        if (StringUtils.isNotBlank(sellerCode))
        {
            String[] ss = sellerCode.split(",");
            param = ss[0];
        }

        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ToMerchantRechargeableCardTotalEarnings> plist = _financialReportService.prepaidCardReport(
                    param, getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ToMerchantRechargeableCardTotalEarnings> plist1 = _financialReportService.prepaidCardReport(
                    param, 1, EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/prepaid/reportSum")
    public Map<String, Object> prepaidCardReportSum(String sellerCode)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            ToMerchantRechargeableCardTotalEarnings t = _financialReportService.prepaidCardReportSum(sellerCode);
            if (t != null)
            {
                // 实际收益累积总额
                map.put("incomeTotal", t.getRechargeableCardRealTotalEarnings());
                // 销售总额
                map.put("salesTotal", t.getRechargeableCardExpenseAmount());
                // 待结算累积总额
                map.put("settlementTotal", t.getRechargeableCardSettlementTotalAmount());
                // 预期收益累积总额
                map.put("expectedTotal", t.getRechargeableCardExpectedEarnings());
                // 应付总额
                map.put("paidTotal", t.getRechargeableCardHandleAmount());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    // 充值卡数据统计明细
    @ResponseBody
    @RequestMapping("/prepaid/detail")
    public Map<String, Object> prepaidCardDetail(String sellerCode, String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            PaginationList<ToMerchantDaliyEarningsDetail> plist = _financialReportService.prepaidCardDetail(sellerCode,
                    beginTime, endTime, getPage(), getPageSize());
            if (plist != null)
            {
                map.put("rows", plist.getRecords());
                map.put("total", plist.getTotalRecord());
            }
            // 导出xls
            PaginationList<ToMerchantDaliyEarningsDetail> plist1 = _financialReportService.prepaidCardDetail(
                    sellerCode, beginTime, endTime, 1, EXPORT_MAX_ROWS);
            List<Excelable> list = new ArrayList<Excelable>(plist1.getTotalRecord());
            list.addAll(plist1.getRecords());
            Map<String, String> alias = new LinkedHashMap<String, String>();
            alias.put("日期", "daliyEarningsDate");
            alias.put("销售总额", "rechargeableCardExpenseAmount");
            alias.put("结算折扣率", "rechargeableCardDicountRate");
            alias.put("应付总额", "rechargeableCardHandleAmount");
            alias.put("实际结算总额", "rechargeableCardSettlementAmount");
            alias.put("实际收益总额", "rechargeableCardRealTotalEarnings");
            String file = ExcelUtil.write(list, session.getServletContext().getRealPath("/") + TEMP_FILE_PATH);
            map.put("fileName", file);
            //
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/prepaid/detailSum")
    public Map<String, Object> prepaidCardDetailSum(String sellerCode, String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            ToMerchantDaliyEarningsDetail t = _financialReportService.prepaidCardDetailSum(sellerCode, beginTime,
                    endTime);
            if (t != null)
            {
                // 收益总额
                map.put("incomeTotal", t.getRechargeableCardRealTotalEarnings());
                // 销售总额
                map.put("salesTotal", t.getRechargeableCardExpenseAmount());
                // 结算总额
                map.put("settlementTotal", t.getRechargeableCardSettlementAmount());
                // 预期收益总额
                map.put("expectedTotal", t.getRechargeableCardExpectedEarnings());
                // 应结算总额
                map.put("paidTotal", t.getRechargeableCardWaitAmount());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @return
     * 
     * @see com.bcinfo.iccard.admin.controller.AdminController#getMouduleCode()
     */

    @Override
    public String getMouduleCode()
    {
        return null;
    }

}
